package com.txt.call;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.library.control.TxtKandy;
import com.example.library.http.HttpRequestClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by DELL on 2017/8/18.
 */

public class UserRegisterActivity extends Activity {
    private final static String TAG=UserRegisterActivity.class.getSimpleName();
    private EditText mUserName;
    private EditText mPasswardName;
    private EditText mEmail;
    private Button mRegisterButoon;
    public final static String USERNAME="username";
    public final static String PASSWARD="passward";
    private ProgressDialog mProgreeDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }
    private void initView() {
        mRegisterButoon= (Button) findViewById(R.id.register);
        mUserName= (EditText) findViewById(R.id.userid);
        mPasswardName= (EditText) findViewById(R.id.passward);
        mEmail= (EditText) findViewById(R.id.email);
        mRegisterButoon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUserName.getText().toString().equals("")){
                    Toast.makeText(UserRegisterActivity.this,"用户输入名字不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }else if (mEmail.getText().equals("")){
                    Toast.makeText(UserRegisterActivity.this,"",Toast.LENGTH_SHORT).show();
                    return;
                } else if (mPasswardName.getText().equals("")){
                    Toast.makeText(UserRegisterActivity.this,"",Toast.LENGTH_SHORT).show();
                    return;

                }
                mProgreeDialog= ProgressDialog.show(UserRegisterActivity.this,null,"正在登录用户，请稍后....",true,false);
                mProgreeDialog.setCancelable(true);
                mProgreeDialog.show();
                Map<String,String> values=new HashMap<String, String>();
                values.put("user_id",mUserName.getText().toString());
                values.put("user_email",mEmail.getText().toString());
                values.put("user_password",mPasswardName.getText().toString());
                TxtKandy.getAccessKandy().registerKandyUser(values, new HttpRequestClient.RequestHttpCallBack() {
                    @Override
                    public void onSuccess(String json) {
                        Log.d(TAG, "onSuccess: json"+json);

                        try {
                            JSONObject jsonObject=new JSONObject(json);
                            if (jsonObject.getString("status").equals("0")){
                                JSONObject resultJson=jsonObject.getJSONObject("result");
                                final String passward=resultJson.getString("user_password");
                                final String user=resultJson.getString("full_user_id");
                                String domainName=resultJson.getString("domain_name");
                                TxtKandy.getAccessKandy().mDomainName=domainName;
                                UserRegisterActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(UserRegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                                        Intent intent=new Intent();
                                        intent.putExtra(USERNAME,user);
                                        intent.putExtra(PASSWARD,passward);
                                        UserRegisterActivity.this.setResult(RESULT_OK,intent);
                                        UserRegisterActivity.this.finish();
                                        mProgreeDialog.dismiss();
                                    }
                                });

                            }else {
                                UserRegisterActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(UserRegisterActivity.this,"注册失败！",Toast.LENGTH_SHORT).show();
                                        mProgreeDialog.dismiss();
                                    }
                                });
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    @Override
                    public void onFail(String err, int code) {
                        Log.d(TAG, "onSuccess: json"+err);
                        UserRegisterActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mProgreeDialog.dismiss();
                            }
                        });
                    }
                });
            }
        });

    }
}
